package com.itfreer.arcgis.edit;

import android.app.Activity;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.itfreer.arcgis.draw.DrawCommand;
import com.itfreer.arcgis.draw.DrawTool;
import com.itfreer.arcgis.draw.DrawToolManager;
import com.itfreer.arcgis.draw.DrawToolType;
import com.itfreer.arcgis.utils.GeometryUtils;
import com.itfreer.arcgis.utils.MapViewUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 屏幕中心点命令
 */
public class ScreenCenterDrawCommand implements DrawCommand
{
    private Class newClass=null;
    public ScreenCenterDrawCommand(Class newClass)
    {
        this.newClass = newClass;
    }
    @Override
    public void Execute(Map<String,Object>params)
    {
        MapView mapView = (MapView) params.get("mapView");
        Activity activity = (Activity) params.get("activity");
        android.graphics.Point screenPoint = MapViewUtils.GetScreenCenterPoint(activity);
        Point point = MapViewUtils.getScreenCenterPoint(screenPoint,activity,mapView);
        DrawTool drawTool = DrawToolManager.GetCurrentTool();
        if(drawTool == null || drawTool.GetGeometry() == null || drawTool.GetGeometry().isEmpty())
        {
            if(drawTool ==null)
                try { drawTool =(DrawTool) newClass.newInstance(); } catch (Exception e) { e.printStackTrace(); }

            if(drawTool == null)
                return;

            point = new Point(point.getX(),point.getY(),mapView.getSpatialReference());
            List<Point> points = new ArrayList<>();
            points.add(point);
            points.add(new Point(point.getX(),point.getY()));
            if(drawTool.getDrawToolType() == DrawToolType.Polygon)
                points.add(new Point(point.getX(),point.getY()));

            Geometry geometry1 = EditToolUtils.getGeometryByDrawTypeAndPoints(drawTool.getDrawToolType(),points,mapView.getSpatialReference());
            drawTool.Draw(mapView, geometry1);
        }
        else
        {
            Geometry geometry  = drawTool.GetGeometry();
            List<Point> points = GeometryUtils.geometryToPoints(geometry);
            points.add(point);
            Geometry geometry1 = EditToolUtils.getGeometryByDrawTypeAndPoints(drawTool.getDrawToolType(),points,mapView.getSpatialReference());
            drawTool.UpdateGeometry(geometry1);
        }
    }
}
